<template>
	<view class="params-page">
		<view v-for="item in paramsList" :key="item.value">
			<view class="params-contain">
				<view class="params-name">
					{{ item.name }}
				</view>
				<view class="params-detail" @click="handleCheck(item)">
					<view class="">
						{{ item.txt }}
					</view>
					<view class="" style="color: #606266; font-size: 28rpx">
						<text v-if="item.type == 'W'">W</text>
						<text v-if="item.type == 'V'">V</text>
					</view>
				</view>
				<view class="params-btn">
					<up-button type="primary" size="default" @click="confirm(item)">确认</up-button>
				</view>
			</view>
		</view>
		<up-picker :columns="actionList" :title="actionTitle" :show="actionSheet" @confirm="(e) => handleTempConfirm(e)"
			@cancel="handleClose" @close="handleClose" :defaultIndex="selected"></up-picker>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app";

	const actionSheet = ref(false);
	const actionTitle = ref("");
	const actionConfirm = ref("");
	const actionList = ref([]);
	const actionDetail = ref({});
	const identity = ref(null)
	const selected = ref([])
	const paramsList = ref([{
			name: "电压保护阈值",
			type: "V",
			value: 1,
		},
		{
			name: "功率保护阈值",
			type: "W",
			value: 2,
		},
		{
			name: "电压校准",
			type: "V",
			value: 3,
		},
		{
			name: "功率校准",
			type: "W",
			value: 4,
		},
	]);
	const details = ref({});
	onLoad((options) => {
		let code = decodeURIComponent(options.id);
		if (code) {
			getDevInfo(code);
		}
		identity.value = uni.getStorageSync("identity")
	});

	const confirm = (item) => {
		if (identity.value == false) {
			// 0  解锁  1 锁后台  2 开放开关机 3 全锁
			if (details.value.devKeyLock == 1 || details.value.devKeyLock == 2 || details.value.devKeyLock == 3) {
				uni.showToast({
					title: '设备已锁定,无法操作',
					icon: 'none'
				})
				return
			}
		}
		let data = {
			id: details.value.id,
			equipNo: details.value.equipNo,
		};
		if (item.name == "电压保护阈值") {
			data.overVoltage = item.txt;
			if (item.txt == "关闭") {
				data.overVoltage = 0;
			} else {
				data.overVoltage = item.txt;
			}
		}
		if (item.name == "功率保护阈值") {
			data.overPower = item.txt;
			if (item.txt == "关闭") {
				data.overPower = 0;
			} else {
				data.overPower = item.txt;
			}
		}
		if (item.name == "电压校准") {
			data.volAdj = item.txt;
		}
		if (item.name == "功率校准") {
			data.powerAdj = item.txt;
		}
		uni.$u.http.post("/wechat/editSend", data).then(res => {
			if (res.success) {
				uni.showToast({
					title: "保存成功",
					icon: "success"
				});
			}
		});

	};
	const handleCheck = (item) => {
		if (identity.value == false) {
			// 0  解锁  1 锁后台  2 开放开关机 3 全锁
			if (details.value.devKeyLock == 1 || details.value.devKeyLock == 2 || details.value.devKeyLock == 3) {
				uni.showToast({
					title: '设备已锁定,无法操作',
					icon: 'none'
				})
				return
			}
		}
		actionTitle.value = item.name;
		actionSheet.value = true;
		actionDetail.value = item;
		if (item.name == "电压保护阈值") {
			actionList.value.push(generateStepArray(90, 300, 10));
			actionList.value[0].unshift("关闭");
		}
		if (item.name == "功率保护阈值") {
			actionList.value.push(generateStepArray(4000, 6000, 100));
			actionList.value[0].unshift("关闭");
		}
		if (item.name == "电压校准") {
			actionList.value.push(generateStepArray(-100, 100, 1));
		}
		if (item.name == "功率校准") {
			actionList.value.push(generateStepArray(-1000, 1000, 10));
		}
		defaultCHeck(actionList.value[0])
	};
	// 默认选中
	const defaultCHeck = (arr) => {
		arr.forEach((it, index) => {
			if (arr[index] == actionDetail.value.txt) {
				selected.value = [index]
			}
		})
	}
	const generateStepArray = (start, end, step) => {
		const result = [];
		for (let i = start; i <= end; i += step) {
			result.push(i);
		}
		return result;
	};
	const handleClose = () => {
		actionSheet.value = false;
		actionList.value = [];
		actionDetail.value = {};
		actionTitle.value = "";
		selected.value = []
	};
	const handleTempConfirm = (e, item) => {
		actionDetail.value.txt = e.value[0];
		handleClose();
	};

	const getDevInfo = async (devId) => {
		let data = {
			id: devId,
		};
		let res = await uni.$u.http.post("/wechat/getDevInfo", data);

		if (res.success) {
			details.value = res.data;
			paramsList.value.forEach((item) => {
				if (item.name == "电压保护阈值") {
					if (details.value.overVoltage == 0) {
						item.txt = '关闭';
					} else {
						item.txt = details.value.overVoltage;
					}
				}
				if (item.name == "功率保护阈值") {
					if (details.value.overPower == 0) {
						item.txt = '关闭';
					} else {
						item.txt = details.value.overPower;
					}
				}
				if (item.name == "电压校准") {
					item.txt = details.value.volAdj || 0;
				}
				if (item.name == "功率校准") {
					item.txt = details.value.powerAdj || 0;
				}
			});
		}
	};
</script>

<style scoped lang="scss">
	.params-page {
		padding-top: 15rpx;
		width: 100%;
		height: 100vh;
		background-color: #f1f1f1;
	}

	.params-contain {
		width: 90vw;
		margin: 20rpx auto;
		display: flex;
		align-items: center;
		font-size: 30rpx;
		line-height: 70rpx;
		padding: 10rpx 0;
		background-color: #fff;
		border-radius: 10px;

		// 名称
		.params-name {
			width: 24vw;
			text-align: left;
			padding-left: 3vw;
			font-size: 28rpx;
			line-height: 28rpx;
		}

		.params-detail {
			width: 30vw;
			display: flex;
			justify-content: space-between;
			border: 1rpx solid #e1e1ee;
			padding: 0 25rpx;
			border-radius: 10rpx;
			margin: 10rpx 4vw 1rpx 10rpx;
			margin-top: 1rpx;

			::v-deep .u-icon {
				padding-top: 6px;
			}
		}

		.params-btn {
			width: 16vw;

			::v-deep .u-button {
				height: 70rpx;
			}
		}
	}
</style>